from flask import Flask, jsonify, request, abort, Response
from AIoT_paddle_tools import *
from time import time
from uuid import uuid4
import json

app = Flask(__name__)

# 申请一个人车猪检测的client和预处理序列
seq_pig, client_pig = paddle_client_create('person_pig_car')
# 申请一个工作服检测的client和预处理序列
seq_cls, client_cls = paddle_client_create('suit')


@app.route('/base_model', methods=['POST'])
def base_model():
    print(request.headers)
    print(type(request.json))
    print(request.json)
    pic = request.json['pic']
    pic_type = request.json['pic_type']
    # 返回一个json
    # [{'type': 1, 'rect': [0.20388753790, 0.33781194686, 0.0879509574, 0.38912479501]},[],[],[]...]
    result, img = paddle_model_person_pig_car(pic, pic_type, seq_pig, client_pig, 0.5, True)

    # 图片裁剪
    for item in result:
        if item['type'] == 1:
            image = paddle_get_suit(item, img)
            # 请求服务器工作服
            color = paddle_model_suit(image, 2, seq_cls, client_cls, 0.5)
            print(color)
    re_data = {}
    re_data["data"] = result
    re_data["color"] = color

    json.dumps(re_data, ensure_ascii=False)
    return str(result)  # 200


@app.route('/working_clothes', methods=['POST'])
def working_clothes():
    return Response()  # 200


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8098)
